package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.detail;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.kaiwukj.android.mcas.di.scope.ActivityScope;
import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.CommentParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.CommentReplyParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.DynamicListParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommentResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DynamicResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.DynamicContract;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

@ActivityScope
public class DynamicPresenter extends BasePresenter<DynamicContract.Model, DynamicContract.View> {

    public static final int COMMENT = 1;//评论
    public static final int ATTENTION = 2;//关注
    public static final int ATTENTION_CANCEL = 3;//取关
    public static final int PRAISE = 4;//点赞
    public static final int PRAISE_CANCEL = 5;//取赞
    public static final int REPORT = 6;//举报
    public static final int BLOCK = 7;//屏蔽
    public static final int BLOCK_USER = 8;//屏蔽
    public static final int SHIELD_CANCEL = 9;//取消屏蔽
    public static final int PUBLISH = 10;//发布
    public static final int DELETE = 11;//删除
    public static final int COMMENT_DELETE = 12;//评论删除
    public static final int COMMENT_REPLY_DELETE = 13;//二级评论删除

    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public DynamicPresenter(DynamicContract.Model model, DynamicContract.View view) {
        super(model, view);
    }

    public void fetchDynamic(DynamicListParams params) {
        mModel.fetchDynamic(params)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<DynamicResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<DynamicResult> result) {
                        mRootView.onFetchDynamic(result.getList());
                    }
                });
    }

    public void fetchTopicDynamic(DynamicListParams params) {
        mModel.fetchTopicDynamic(params)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<DynamicResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<DynamicResult> result) {
                        mRootView.onFetchDynamic(result.getList());
                    }
                });
    }

    public void getDynamicById(int id) {
        mModel.getDynamicById(id)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<DynamicResult>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull DynamicResult result) {
                        mRootView.onGetDynamic(result);
                    }
                });
    }

    public void delDynamic(int id) {
        mModel.delDynamic(id)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, DELETE) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.hideLoading();
                    }
                });
    }

    public void fetchComment(int dynamicId, int startId, int pageSize) {
        mModel.fetchComment(dynamicId, startId, pageSize)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<CommentResult>>() {
                    @Override
                    public void onSuccess(@NonNull ListResp<CommentResult> result) {
                        mRootView.onGetComment(result.getList(), false);
                    }
                });
    }

    public void fetchReply(int replyId, int startId, int pageSize) {
        mModel.fetchReply(replyId, startId, pageSize)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<CommentResult>>() {
                    @Override
                    public void onSuccess(@NonNull ListResp<CommentResult> result) {
                        mRootView.onGetComment(result.getList(), true);
                    }
                });
    }

    public void comment(CommentParams params) {
        mModel.comment(params)
                .doOnSubscribe(act -> mRootView.showLoading("正在发送..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, COMMENT) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        LogUtils.i("评论成功");
                        mRootView.onCommentResult(result, params);
                    }
                });
    }

    public void commentReply(CommentReplyParams params, int pos) {
        mModel.commentReply(params)
                .doOnSubscribe(act -> mRootView.showLoading("正在发送"))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, COMMENT) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        LogUtils.i("评论成功");
                        mRootView.onReplyResult(pos, result, params);
                    }
                });
    }

    public void delComment(int type, int id) {
        mModel.delComment(type, id)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, type == 1 ? COMMENT_DELETE : COMMENT_REPLY_DELETE) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        LogUtils.i("删除成功");
                        // mRootView.hideLoading();
                    }
                });
    }

    public void attention(int id) {
        mModel.attention(id)
                .doOnSubscribe(act -> mRootView.showLoading("正在提交..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, ATTENTION) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        LogUtils.i("关注成功");
                    }
                });
    }

    public void cancelAttention(int id) {
        mModel.cancelAttention(id)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, ATTENTION_CANCEL) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        LogUtils.i("取消关注");
                    }
                });
    }

    public void praise(int id) {
        mModel.praise(id)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, PRAISE) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        LogUtils.i("点赞成功");
                    }
                });
    }

    public void cancelPraise(int id) {
        mModel.cancelPraise(id)
                .doOnSubscribe(act -> mRootView.showLoading("正在请求..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, PRAISE_CANCEL) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        LogUtils.i("取消点赞");
                    }
                });
    }

    public void report(int id) {
        //mModel
    }

    /**
     * @param id   主键
     * @param user 屏蔽类型ID，1动态, 2用户
     */
    public void blockUserOrDynamic(int id, boolean block, boolean user) {
        mModel.shield(id, block, user)
                .doOnSubscribe(act -> mRootView.showLoading("正在请求..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, user ? BLOCK_USER : BLOCK) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        LogUtils.i("屏蔽");
                        mRootView.showMessage("屏蔽成功");
                    }
                });
    }

}
